package com.xnck.example.jertest.filter;

import com.xnck.example.jertest.service.TestService;
import com.xnck.example.jertest.util.BeanUtil;

import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 测试用过滤器
 */
public class TestFilter implements Filter {

    private TestService testService;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Enumeration<String> parames = filterConfig.getInitParameterNames();
        while (parames.hasMoreElements()) {
            String paramName = parames.nextElement();
            String paramValue = filterConfig.getInitParameter(paramName);
            System.out.println("过滤器传入的参数为:" + paramName + ":" + paramValue);
        }
        testService = BeanUtil.getBean("testService", TestService.class);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println(testService.getTestMessage());
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
